﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SubSonic;
using CXDB;
using Coolite.Ext.Web;
/// <summary>
///CXFileUpload 的摘要说明
/// </summary>
public class CXFileUpload
{
	public CXFileUpload()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    public static void Sv(FileUploadField f,Guid g)
    {
        if (f.PostedFile.ContentLength != 0)
        {
             
            string filename = f.PostedFile.FileName.ToLower();
            string fname = filename.Substring(filename.LastIndexOf("\\")+1, filename.Length - filename.LastIndexOf("\\")-1).ToString();
             
            string guid = Guid.NewGuid().ToString();
            string path = HttpContext.Current.Server.MapPath("~/uploadfile/");
            if (filename.Substring(filename.IndexOf(".")) == ".zip")
            {
                f.PostedFile.SaveAs(path + guid + ".zip");
                CxFile cx = new CxFile();
                cx.FileID = new Guid(guid);
                cx.Guid = g;
                cx.InTime = DateTime.Now;
                cx.Filename = fname;
                cx.Filetype = "zip";
                cx.Filepath = "/uploadfile/" + guid + ".zip";
                cx.Save();
            }
            if (filename.Substring(filename.IndexOf(".")) == ".rar")
            {
                f.PostedFile.SaveAs(path + guid + ".rar");
                CxFile cx = new CxFile();
                cx.FileID = new Guid(guid);
                cx.Guid = g;
                cx.InTime = DateTime.Now;
                cx.Filename = fname;
                cx.Filetype = "rar";
                cx.Filepath = "/uploadfile/" + guid + ".rar";
                cx.Save();
            }
        }
    }
    public static string show(Guid g)
    {
        string str="";
        Query q =new Query(CxFile.Schema);
        q.AddWhere("guid", g);
        IDataReader dr = q.ExecuteReader();
        if(dr.Read())
        {
            str = "~/uploadfile/"+ dr["fileid"].ToString() + "." + dr["filetype"].ToString();
            str = str + "$" + dr["filename"].ToString();
        }
        else
        {
            str = "$";
        }
        return str;
    }
    public static void Del(string guid)
    {
        string fileid = "";
        string filepath = "";
        Query q = new Query(CxFile.Schema);
        q.SetSelectList("filepath,fileid");
        q.AddWhere("guid", guid);
        IDataReader dr = q.ExecuteReader();
        if (dr.Read())
        {
            fileid = dr["fileid"].ToString();
            filepath = "~" + dr["filepath"].ToString();
            CxFile.Delete("fileid", fileid);
            System.IO.File.Delete(HttpContext.Current.Server.MapPath(filepath));
        }
        
                 
    }
}
